//
//  SEGVisitorAccessHistoryViewController.m
//  ExcellentHome
//
//  Created by 乔杨 on 2020/6/4.
//
//访客历史列表界面
#import "SEGVisitorAccessHistoryViewController.h"
#import "SEGVisitorAccessViewController.h"

#import "SEGVisitorAccessHistoryViewCell.h"

#import "SEGVisitorAccessHistoryViewModel.h"

@interface SEGVisitorAccessHistoryViewController ()<UITableViewDelegate, UITableViewDataSource>
 
@property (nonatomic, strong)UITableView *tableView;

/// viewModel
@property (nonatomic, strong) SEGVisitorAccessHistoryViewModel *viewModel;

@end

@implementation SEGVisitorAccessHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"访客历史";
    self.view.backgroundColor = kColor_bg;
    [self.view addSubview:self.tableView];
    
    WeakSelf;
    [self.tableView addWindmillWithHeaderRefreshBlock:^{
        weakSelf.viewModel.pageNo = 1;
        [weakSelf loadData];
    } footerRefreshBlock:^{
        [weakSelf loadData];
    }];
    [self.tableView beginHeaderRefreshing];
}
- (void)loadData {
    @weakify(self);
    [[self.viewModel.command execute:@""] subscribeNext:^(NSArray *data) {
        @strongify(self);
        [self.tableView stopAnimatingWindmill];
        if (data.count == 0) {
            [[SEGUhomeMediator sharedInstance] seg_showCustomProgressImage:@{@"view":self.view}];
        } else {
            [self.tableView reloadData];
        }
    } error:^(NSError * _Nullable error) {
        [self.tableView stopAnimatingWindmill];
        [SEGAlertView showTmepMessageWithMessage:error.localizedFailureReason];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.historyArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGVisitorAccessHistoryViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kVisitorAccessHistoryViewCellIdentifier forIndexPath:indexPath];
    SEGVisitorAccessHistoryModel *model = self.viewModel.historyArray[indexPath.row];
    cell.nameLable.text = [NSString stringWithFormat:@"访客：%@",model.visitorName];
    cell.timeLabel.text = [NSString stringWithFormat:@"申请时间：%@",model.createTime];
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //跳转到访客历史详情
    SEGVisitorAccessViewController *detailVC = [[SEGVisitorAccessViewController alloc] init];
    detailVC.model = self.viewModel.historyArray[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}
// 设置分割线
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    UIEdgeInsets insets = UIEdgeInsetsZero;//屏幕等宽分割线
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:insets];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:insets];
    }
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:insets];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:insets];
    }
}

#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = kColor_bg;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = kColor_L2;
        _tableView.rowHeight = 68.0;
        [_tableView registerClass:[SEGVisitorAccessHistoryViewCell class] forCellReuseIdentifier:kVisitorAccessHistoryViewCellIdentifier];
        UIView *footView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.tableFooterView = footView;
    }
    return _tableView;
}
- (SEGVisitorAccessHistoryViewModel *)viewModel {
    if (!_viewModel) _viewModel = [[SEGVisitorAccessHistoryViewModel alloc] init];
    return _viewModel;
}

@end

